﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ADNLib
{
    [Serializable]
	public class XGroup : XObject
	{
		private List<XObject> children = new List<XObject>();

		[XmlElement("SHAPE")]
		public List<XObject> Children
		{
			get
			{
				return children;
			}
			set
			{
				children = value;
			}
		}

		[XmlIgnore]
		public override RectangleF Bounds
		{
			get
			{
				return GetBounds();
			}
		}

        private RectangleF GetBounds()
        {
            //loop through children., getting bounds. create max rect
            RectangleF maxRect = RectangleF.Empty;
            for (int i = 0; i < children.Count; i++)
            {
                if (i == 0) { maxRect = children[i].Bounds; }
                else
                {
                    maxRect = RectangleF.Union(children[i].Bounds, maxRect);
                }
            }
            return maxRect;
        }

        public override bool HitTest(Graphics g, PointF p)
        {
            for (int i = 0; i < children.Count; i++)
            {
                if (children[i].HitTest(g, p)) { return true; }
            }

            return false;
        }

        public override void Draw(Graphics g)
        {
            for (int i = 0; i < children.Count; i++)
            {
                children[i].Draw(g);
            }
        }

        public override void DrawGhost(Graphics g)
        {
            for (int i = 0; i < children.Count; i++)
            {
                children[i].DrawGhost(g);
            }
        }

        public override void Move(PointF amount)
        {
            for (int i = 0; i < children.Count; i++)
            {
                children[i].Move(amount);
            }
        }

        public override void Resize(SizeDirection direction, PointF amount)
        {
            for (int i = 0; i < children.Count; i++)
            {
                children[i].Resize(direction, amount);
            }
        }

        public override void DrawPathMarkers(Graphics g, List<PathPoint> selMarkers)
        {
            for (int i = 0; i < children.Count; i++)
            {
                children[i].DrawPathMarkers(g, selMarkers);
            }
        }
	}
}
